﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Web;

namespace WebApplication1.Common
{
    public class TxtCommon
    {
        /// <summary>
        /// 清楚文本的html标签
        /// </summary>
        /// <param name="Content"></param>
        /// <returns></returns>
        public static string ClearHtml(string Content)
        {
            Content = TxtCommon.Zxj_ReplaceHtml("&nbsp;", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("&#[^>]*;", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?marquee[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?object[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?param[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?embed[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?table[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml(" ", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?tr[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?th[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?p[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?a[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?img[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?tbody[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?li[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?span[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?div[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?th[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?td[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?script[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("(javascript|jscript|vbscript|vbs):", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("on(mouse|exit|error|click|key)", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("<\\?xml[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("<\\/?[a-z]+:[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?font[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?b[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?u[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?i[^>]*>", "", Content);
            Content = TxtCommon.Zxj_ReplaceHtml("</?strong[^>]*>", "", Content);
            return Content;
        }

        private static string Zxj_ReplaceHtml(string patrn, string strRep, string content)
        {
            if (string.IsNullOrEmpty(content))
            {
                content = "";
            }
            return new Regex(patrn, RegexOptions.IgnoreCase).Replace(content, strRep);
        }
    }
}